# -*- coding:utf-8 -*-
from flask import jsonify, Blueprint, Request, request

__author__ = 'harumonia'


# 实例化一个蓝图
stage = Blueprint('stage', __name__)



@stage.route('/')
def index():
    return '<h1>假装我是首页</h1>'


@stage.route('/hello/')
def hello_world():
    return 'Hello World!'


# 路由的另一个注册方式
stage.add_url_rule('/nihao/', view_func=hello_world)


@stage.route('/view_func_1')
def my_first_view_func():
    '''
    我的第一个视图函数,爬取博客首页的文章标题
    :return:标题列表
    '''
    import requests
    from bs4 import BeautifulSoup
    req = requests.get('http://harumonia.top')
    soup = BeautifulSoup(req.content, 'lxml')
    ll = soup.find_all(class_='index-post-title')
    name_ll = [foo.text for foo in ll]
    return jsonify(name_ll)
    # return json.dumps(name_ll, ensure_ascii=False)



@stage.route('/view_for_request_1/<user>')
def request_view_func_1(user):
    # print(user)
    return user


# stage.add_url_rule('/view_for_request_2',view_func=request_view_func_1)
# '''http://127.0.0.1:5000/view_for_request_2?zxj'''
# '''报错'''

@stage.route('/view_for_request_2')
def request_view_func_2():
    user = request.args['user']
    return user
